package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Tipotelefono;
import services.TipoTelefonoService;

@FacesConverter("tipotelefonoConverter")
public class TipoTelefonoConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
		if (value != null && value.trim().length() > 0) {
			TipoTelefonoService service = (TipoTelefonoService) fc
					.getExternalContext().getApplicationMap()
					.get("tipoTelefonoService");
			Iterator<Tipotelefono> i = service.getListaTiposTelefonos()
					.iterator();
			while (i.hasNext()) {
				Tipotelefono t = (Tipotelefono) i.next();
				if (t.getIdTipoTelefono() == Integer.parseInt(value))
					return t;
			}
			return null;
		} else {
			return null;
		}
	}

	public String getAsString(FacesContext fc, UIComponent uic, Object object) {
		if (object != null) {
			return String.valueOf(((Tipotelefono) object).getIdTipoTelefono());
		} else {
			return null;
		}
	}
}
